<template>
	<view>
		<!-- 搜索 -->
		<mySearch></mySearch>
		<view class="scroll-view-container">
			<!-- 左侧的滚动视图区域 -->
			<scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<view class="left-scroll-view-item" :class="{active1:index==active}" v-for="(item,index) in cateList"
					:key="item.cat_id" @click="cate_item(item.children,index)">
					<view class="active " v-show="index==active"></view>
					{{item.cat_name}}
				</view>

			</scroll-view>
			<!-- 右侧的滚动视图区域 -->
			<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<view class="left-scroll-view-item" v-for="(cate_children,index) in cate_children" :key="cate_children.cat_id">
					<view class="cate_children_name"
						v-if="  cate_children.children || cate_children.cat_icon.includes('https') ">
						/{{cate_children.cat_name}}/
					</view>
					<view class="cat_list_item" v-if='cate_children.cat_icon'>
						<view class="cat_list" v-if="cate_children.cat_icon.includes('https')">
							<image mode="aspectFit" :src="cate_children.cat_icon"></image>

						</view>
					</view>

					<view class="cat_list_item">

						<view class="cat_list" v-for="(item,index) in cate_children.children" :key="item.cat_id">
							<navigator :url="`/subGoodsDetail/goods_list/goods_list?cid=${item.cat_id} `">
								<image mode="aspectFit" :src="item.cat_icon"></image>
								<view class="cat_name">
									{{item.cat_name}}
								</view>
							</navigator>
						</view>

					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cateList: [], // 分类数据列表
				cate_children: [], //右边item
				active: 0, //控制点击样式
				wh: 0, //可使用窗口高度 = 屏幕高度 - navigationBar高度 - tabBar 高度 - 自定义的search组件高度
			};
		},
		onLoad() {

			this.getCateList()
			const {
				windowHeight
			} = uni.getSystemInfoSync() //获取设配信息(高度)，tabbar不挡住
			this.wh = windowHeight - 50
		},
		onShow() {
			uni.$setNavigationBarColor('tabBar')
			//tabBar数字图标显示
			uni.$setTabBarBadge()
			uni.$logon()
		},
		methods: {
	
			// 分类数据列表
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				if (res.meta.status !== 200) return uni.$showToast(res.meta.msg, "error")
				// 转存数据
				this.cateList = res.message

				this.cate_item(this.cateList[0].children, 0)
				uni.$showToast()

			},

			//右侧图片
			cate_item(cate_children, index) {
				this.active = index
				//因图片连接问题，替换
				cate_children.forEach(item => {
					if (item.cat_icon) {
						item.cat_icon = item.cat_icon.replace("dev", "web")
					}
					if (item.children) {
						item.children.forEach(item_icon => {
							item_icon.cat_icon = item_icon.cat_icon.replace("dev", "web")
						})
					}
				})

				this.cate_children = cate_children
			},
		},

	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.active1 {
			background-color: white !important;
			color: red;
		}

		.left-scroll-view {
			width: 240rpx;

			.left-scroll-view-item {
				background-color: #f7f7f7;
				line-height: 120rpx;
				font-size: 24rpx;
				text-align: center;
				position: relative;

				.active {
					position: absolute;
					top: 30rpx;
					width: 6rpx;
					height: 60rpx;
					background-color: #c00000;
				}
			}
		}

		.cate_children_name {
			font-weight: bold;
			padding: 25px 0 40px 0;
			font-size: 30rpx;
			display: flex;
			justify-content: center;
		}

		.cat_list_item {
			display: flex;
			flex-wrap: wrap;

			// margin-top: -10px;
			.cat_list {
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 33.3%;
				margin-bottom: 10px;
			}

			image {
				width: 120rpx;
				height: 120rpx;
			}

			.cat_name {
				font-size: 24rpx;
			}
		}
	}
</style>